<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="3" :xs="24">
        <div class="head-container">
          <el-input
            v-model="name"
            placeholder="请输入区域名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="userOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="21" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户名称" prop="contacts">
            <el-input
              v-model="queryParams.contacts"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              v-model="queryParams.phone"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="审批状态">
           <el-select v-model="queryParams.checkStatus" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户编号" align="center" prop="userId"  />
          <el-table-column label="用户头像" align="center"  prop="headUrl">
             <!-- 图片的显示 -->
                 <template   slot-scope="scope">            
                    <img :src="scope.row.headUrl"  min-width="70px" height="70px" 
                           @click="previewPic(scope.row.headUrl)" />
                 </template>   
          </el-table-column>
          <el-table-column label="用户名称" align="center" prop="contacts"  />
          <el-table-column label="区域角色" align="center" >
            <template slot-scope="scope">
              <span v-if="scope.row.name==null"></span>
              <span v-else>{{scope.row.areaName}}-{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" align="center" prop="nickName"  />
          <el-table-column label="联系方式" align="center"  prop="phone"  />
          <el-table-column label="创建时间" align="center"  prop="createTime" />
          <el-table-column label="审批时间" align="center"  prop="checkTime" />
          <el-table-column label="证书" align="center"  prop="certificateUrl">
             <template   slot-scope="scope">            
                    <img :src="scope.row.certificateUrl"  min-width="70px" height="70px" 
                           @click="previewPic(scope.row.certificateUrl)" />
                 </template>   
          </el-table-column>
          <el-table-column label="审批人" align="center"  prop="approverId">
            <template slot-scope="scope">
              <span v-if="scope.row.approverId==0">管理员</span>
              <span v-else></span>
            </template>
          </el-table-column>
          <el-table-column label="审批操作" align="center"  prop="checkStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.checkStatus==0">
              <el-button size="mini" round type="primary">通过</el-button>
              <el-button size="mini" round type="warning">驳回</el-button>
              </span>
             <span v-else>
              <el-button size="mini" round type="success">撤销</el-button>
             </span>
            </template>
          </el-table-column>

          <el-table-column label="审批状态" align="center"  prop="checkStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.checkStatus==0">审批中</span>
              <span v-if="scope.row.checkStatus==1">通过</span>
              <span v-if="scope.row.checkStatus==2">驳回</span>
              <span v-if="scope.row.checkStatus==3">撤销</span>
            </template>
          </el-table-column>
          <el-table-column label="总积分" align="center"  prop="integral" />
          <el-table-column label="总工时" align="center"  prop="laborHour" />
          <el-table-column label="状态" align="center"  prop="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

        <!-- 图片预览   放在表格外面，这是个弹出框-->
      <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->

    <!-- 添加或修改志愿者义工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入微信用户id" />
        </el-form-item>
        <el-form-item label="协会id" prop="asId">
          <el-input v-model="form.asId" placeholder="请输入协会id" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="证书编号" prop="certificateNo">
          <el-input v-model="form.certificateNo" placeholder="请输入证书编号" />
        </el-form-item>
        <el-form-item label="区域id" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区域id" />
        </el-form-item>
        <el-form-item label="内容" prop="customField">
          <el-input v-model="form.customField" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="驳回理由" prop="rejectReason">
          <el-input v-model="form.rejectReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审批人id" prop="approverId">
          <el-input v-model="form.approverId" placeholder="请输入审批人id" />
        </el-form-item>
        <el-form-item label="审核时间" prop="checkTime">
          <el-date-picker clearable
            v-model="form.checkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入总积分" />
        </el-form-item>
        <el-form-item label="总工时" prop="laborHour">
          <el-input v-model="form.laborHour" placeholder="请输入总工时" />
        </el-form-item>
        <el-form-item label="背景url" prop="bgUrl">
          <el-input v-model="form.bgUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="上传证书url" prop="certificateUrl">
          <el-input v-model="form.certificateUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="form.role" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="自定义数据" prop="customData">
          <el-input v-model="form.customData" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/Community/user";
import { getAreaList} from "@/api/Community/area";
export default {
  name: "User",
  data() {
    return {
      dialogVisible: false,
      previewpic: "",  
      //管理人员树形
      userOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //区域名称
      name:undefined,
     // 日期范围
      dateRange: [],
      // 志愿者义工信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      options:[
        {
          value:0,
          label:"审批中"
        },
        {
           value:1,
          label:"通过"
        },
        {
           value:2,
          label:"驳回"
        },
        {
           value:3,
          label:"撤销"
        }
        
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        asId: null,
        type: null,
        contacts: null,
        phone: null,
        certificateNo: null,
        areaId: null,
        status: null,
        customField: null,
        checkStatus: null,
        rejectReason: null,
        approverId: null,
        checkTime: null,
        integral: null,
        laborHour: null,
        bgUrl: null,
        certificateUrl: null,
        role: null,
        customData: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "微信用户id不能为空", trigger: "blur" }
        ],
        asId: [
          { required: true, message: "协会id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选区域树
    name(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeList();
  },
  //图片放大
  previewPic(url) {
    this.previewpic = url;
    this.dialogVisible = true;
        },
  methods: {
    /** 查询志愿者义工信息列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(res => {
        console.info(res);
        this.userList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    //获取区域树形结构
    getTreeList(){
      getAreaList(this.queryParams).then(res=>{
        this.userOptions=res.data;
      })
    },
       // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
       // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        asId: null,
        type: null,
        contacts: null,
        phone: null,
        certificateNo: null,
        areaId: null,
        createTime: null,
        updateTime: null,
        status: 0,
        customField: null,
        checkStatus: 0,
        rejectReason: null,
        approverId: null,
        checkTime: null,
        integral: null,
        laborHour: null,
        bgUrl: null,
        certificateUrl: null,
        role: null,
        customData: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加志愿者义工信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改志愿者义工信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除志愿者义工信息编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('Community/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
